
import userService from "../service/user.service.js";

const adminHandler = async (req, res, next) => {
    const userId = req.user?.id;
    if (!userId) {
        return res.status(401).json({ message: "请登录" });
    }
    try {
        const user = await userService.getUserById(userId);
        if (!user) {
            return res.status(401).json({ message: "登录状态异常，请重新登录" });
        }
        if (user.role !== 'ADMIN') {
            return res.status(401).json({ message: "您不是管理员" });
        }
        next();
    } catch (error) {
        return res.status(401).json({ message: "登录状态异常，请重新登录" });
    }
};

export default adminHandler;
